package com.idanchuang.component.base.actuator.tomcat;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;

/**
 * @author yjy
 * Created at 2021/6/3 2:22 下午
 */
public class TomcatHealthIndicator extends AbstractHealthIndicator {

    private final MeterRegistry meterRegistry;

    public TomcatHealthIndicator(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        try {
            Gauge gauge = meterRegistry.get("tomcat.threads.config.max").gauge();
            double allThreadsCount = gauge.value();
            Gauge busyThreads = meterRegistry.get("tomcat.threads.busy").gauge();
            double busyThreadsCount = busyThreads.value();
            builder.up()
                    .withDetail("busyThreads", busyThreadsCount)
                    .withDetail("maxThreads", allThreadsCount);
        } catch (Exception e) {
            builder.up().withDetail("cause", e.getMessage());
        }
    }

}
